import GameObject from "./GameObject"
import Utils from "./utils"
import GameConst from "./GameConst";
export default class Tips extends GameObject {
    constructor(text, x,y) {
        super();
        this.speed = new Laya.Point;
        this.targetPos = new Laya.Point;
        this.diff = new Laya.Point;
        this.isHiding = false;
        this.isFinished = false;
        this.score = 0;
      
        var r = Utils.createBitmap(text);
        r.scaleX = r.scaleY = 1;
        this.sprite = r;
        this.resized = false;
        this.init(x,y);
    }

    resize() {
        if(this.resized){
           return; 
        }
        var s = this.sprite.getBounds();
        if(s.width>0){
            this.sprite.pivotX = Math.ceil(s.width / 2);
            this.sprite.pivotY = Math.ceil(s.height);
            this.resized = true;
        }
        
    }

    update(t) {
        this.resize();
        super.update(t);
        if (!this.isHiding) {
            this.sprite.x += this.speed.x * t;
            this.sprite.y += this.speed.y * t;
            this.diff.x = this.targetPos.x - this.sprite.x;
            this.diff.y = this.targetPos.y - this.sprite.y;
            var r = Utils.distanceBetweenPoints(0, 0, this.diff.x, this.diff.y);
            this.diff.x /= r;
            this.diff.y /= r;
            var i = .8 * 60 * 60;
            this.diff.x *= i;
            this.diff.y *= i;
            if (this.speedModulo > 0) {
                this.speed.x += this.diff.x * t;
                this.speed.y += this.diff.y * t;
                var r = Utils.distanceBetweenPoints(0, 0, this.speed.x, this.speed.y);
                if (r > this.speedModulo) {
                    var s = this.speedModulo / r;
                    this.speed.x *= s;
                    this.speed.y *= s;
                }
            } else
                this.speed.x = this.speed.y = 0;
            if (Math.abs(this.speedModulo) < 1e-5 && !this.isHiding) {
                this.isHiding = true;
                    Laya.Tween.to(this.sprite, {
                        alpha: .2
                    }, 400, Laya.Ease.quartIn, Laya.Handler.create(this, this.finishMovement));

            }
        }
    }

    init(x, y) {
        this.speed.x = this.speed.y = this.targetPos.x = this.targetPos.y = this.diff.x = this.diff.y = 0;
        this.isHiding = this.isFinished = false;
        this.sprite.alpha = 1;
        if (GameConst.level)
            GameConst.level.hud.addTip(this);
        this.sprite.x = x;
        this.sprite.y = y;
        this.sprite.visible = true;
        this.isDestroyed = false;
        this.speedModulo =  15 * 60;
        this.speed.y = -this.speedModulo;
        // var r = 0;
        
        // var i = point.y < 3 ? 110 : point.y > GameConst.level.fieldHeight - 3 ? 70 : Math.random() > .5 ? 110 : 70;
        // r = Utils.lerp(-15, 15, Math.random()) + (point.x < 3 ? i : point.x > GameConst.level.fieldWidth - 3 ? -i : Math.random() > .5 ? i : -i);
      
        // Utils.rotatePoint(this.speed, r);
        Laya.Tween.to(this, {
            speedModulo: 0
        }, 750, Laya.Ease.quadOut);

        this.targetPos.x = x;
        this.targetPos.y = y;
        return this;
    };

    release() {
        Laya.Tween.clearAll(this.sprite);
        Laya.Tween.clearAll(this);
        Utils.removeClip(this.sprite);
        this.sprite.visible = false;
        this.isDestroyed = true;
    };

    finishMovement() {
        if (this.isFinished)
            return;
        this.isFinished = true;
        this.release();
    };
    destroy() {
    };
}